package felix.rule.service.actors.chain;

import felix.actor.core.TActor;
import felix.actor.core.TActorCtx;
import felix.actor.core.TActorId;
import felix.actor.core.msg.TActorMsg;
import felix.actor.core.msg.TMsgType;
import felix.rule.data.plugin.ComponentLifecycleMsg;
import felix.rule.service.actors.ActorSystemContext;
import felix.rule.service.actors.ComponentActor;
import felix.rule.service.actors.ContextBasedCreator;
import felix.rule.service.actors.TEntityActorId;
import lombok.extern.slf4j.Slf4j;

import static felix.rule.data.msg.MessageType.*;


@Slf4j
public class RuleNodeActor extends ComponentActor<RuleNodeActorMessageProcessor> {

    private final String ruleChainName;
    private final String ruleChainId;
    private final String ruleNodeId;

    private RuleNodeActor(ActorSystemContext systemContext, String tenantId, String ruleChainId, String ruleChainName, String ruleNodeId) {
        super(systemContext, tenantId, ruleNodeId);
        this.ruleChainName = ruleChainName;
        this.ruleChainId = ruleChainId;
        this.ruleNodeId = ruleNodeId;
    }

    @Override
    protected RuleNodeActorMessageProcessor createProcessor(TActorCtx ctx) {
        return new RuleNodeActorMessageProcessor(ruleId, this.ruleChainName, ruleNodeId, systemContext, ctx.getParentRef(), ctx);
    }

    @Override
    protected boolean doProcess(TActorMsg msg) {
        TMsgType msgType = msg.getMsgType();
        if (RULE_CHAIN_TO_RULE_MSG.equals(msgType)) {
            onRuleChainToRuleNodeMsg((RuleChainToRuleNodeMsg) msg);
        } else if (RULE_TO_SELF_MSG.equals(msgType)) {
            onRuleNodeToSelfMsg((RuleNodeToSelfMsg) msg);
        } else if (COMPONENT_LIFE_CYCLE_MSG.equals(msgType)) {
            onComponentLifecycleMsg((ComponentLifecycleMsg) msg);
        } else {
            return false;
        }
        return true;
    }

    private void onRuleNodeToSelfMsg(RuleNodeToSelfMsg msg) {
        if (log.isDebugEnabled()) {
            log.debug("[{}][{}][{}] Going to process rule msg: {}", ruleChainId, id, processor.getComponentName(), msg.getMsg());
        }
        try {
            processor.onRuleToSelfMsg(msg);
            increaseMessagesProcessedCount();
        } catch (Exception e) {
            logAndPersist("onRuleMsg", e);
        }
    }

    private void onRuleChainToRuleNodeMsg(RuleChainToRuleNodeMsg msg) {
        if (log.isDebugEnabled()) {
            log.debug("[{}][{}][{}] Going to process rule msg: {}", ruleChainId, id, processor.getComponentName(), msg.getMsg());
        }
        try {
            processor.onRuleChainToRuleNodeMsg(msg);
            increaseMessagesProcessedCount();
        } catch (Exception e) {
            logAndPersist("onRuleMsg", e);
        }
    }

    @Override
    protected long getErrorPersistFrequency() {
        return systemContext.getRuleNodeErrorPersistFrequency();
    }

    public static class ActorCreator extends ContextBasedCreator {

        private final String tenantId;
        private final String ruleChainId;
        private final String ruleChainName;
        private final String ruleNodeId;

        public ActorCreator(ActorSystemContext context, String tenantId, String ruleChainId, String ruleChainName, String ruleNodeId) {
            super(context);
            this.tenantId = tenantId;
            this.ruleChainId = ruleChainId;
            this.ruleChainName = ruleChainName;
            this.ruleNodeId = ruleNodeId;

        }

        @Override
        public TActorId createActorId() {
            return new TEntityActorId(ruleNodeId, RuleNodeActor.class.getSimpleName());
        }

        @Override
        public TActor createActor() {
            return new RuleNodeActor(context, tenantId, ruleChainId, ruleChainName, ruleNodeId);
        }
    }

}
